package com.xywl.escort.view.message;

import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.MessageResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created by 32422 on 2018/3/7.
 */

public class MessagePresenter implements MessageContract.Presenter {
    private MessageContract.View mView;
    private ApiServer mApiServer;

    @Inject
    MessagePresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        this.mView = null;
    }

    @Override
    public void takeView(MessageContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadMessage(String userId, int pageNum) {
        BaseReq req = new BaseReq();
        req.userId = userId;
        req.pageNum = pageNum;
        mApiServer.messageList(req)
                .compose(RxUtil.<MessageResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<MessageResp>bindToLife())
                .subscribe(new BaseSubscriber<MessageResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadMessageFailure("");
                    }

                    @Override
                    public void onNext(MessageResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            mView.updateMessage(resp.data);
                        } else {
                            mView.loadMessageFailure("");
                        }
                    }
                });
    }

}
